package com.linkage.educloud.ah.mvp.presenter;

import android.content.Context;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.MyApplication;
import com.linkage.educloud.ah.bean.AccountData;
import com.linkage.educloud.ah.config.AccountConfig;
import com.linkage.educloud.ah.data.WsContentResovler;
import com.linkage.educloud.ah.http.Method;
import com.linkage.educloud.ah.http.RequestUtil;
import com.linkage.educloud.ah.mvp.contract.ISplashContract;
import com.linkage.educloud.ah.mvp.model.SplashModel;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;
import com.zhouyou.http.subsciber.BaseSubscriber;
import com.zzhou.common.library.util.LogUtils;
import com.zzhou.common.library.util.SystemTool;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observable;
import io.reactivex.functions.Function;

/**
 * <desc>
 * Created by The Moss on 2018/8/7.
 */

public class SplashPresenter extends ISplashContract.IPresenter {
    ISplashContract.ISplashModel iModel;
    Context mContext;

    public SplashPresenter(Context context,ISplashContract.IView iView) {
        super(iView);
        this.mContext = context;
        this.iModel = new SplashModel();
    }

    @Override
    public void getSplashBg(int type) {
        request(RequestUtil.getSplashParams(type), new BaseSubscriber<String>() {
            @Override
            public void onError(ApiException e) {
                LogUtils.d("error ", e.getMessage());
            }

            @Override
            public void onNext(String s) {
                try {
                    JSONObject json = new JSONObject(s);
                    if (json.getInt("result") == 1) {
                        String pic_path = json.optString("pic_path");
                        int pic_key = json.optInt("pic_key");
                        ((ISplashContract.IView)iView).onGetSplahResult(pic_path, pic_key);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    @SuppressWarnings("unchecked")
    @Override
    public void autoLogin(final String account, final String pass, int userType) {
        String IMEI = SystemTool.getPhoneIMEI(mContext);
        if (IMEI == null) {
            IMEI = android.os.Build.MODEL;
        }
        if (IMEI == null) {
            IMEI = "";
        }
        String verNo = SystemTool.getAppVersionCode(mContext)+"";
        String netType = SystemTool.getNetworkType(mContext);
        String info = android.os.Build.MODEL + ","
                + android.os.Build.VERSION.RELEASE + ","
                + Constants.DEVICE + verNo + "," + netType + "," + IMEI;
        Observable<String> observable = iModel.request(Method.POST, false, RequestUtil.autoLogin(userType,account,pass,info));
        observable.compose(iView.bindLifecycle())
                .map(new Function<String, Integer>() {
                    @Override
                    public Integer apply(String s) throws Exception {
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getInt("ret") == 0) {
                                AccountData accountData = AccountData.fromJSONObject(json.getJSONObject("data"));
                                WsContentResovler resovler = MyApplication.getInstance().getResovler();
                                accountData.setLoginAccount(account);
                                accountData.setLoginpwd(pass);
                                accountData.setLoginUser(1);
                                accountData.setlastLoginTime(System.currentTimeMillis());
                                resovler.clearLoginAccount();
                                resovler.insertOrUpdateAccount(accountData.getLoginAccount(),accountData);
                                AccountConfig.notifyAccountChanged();
                                return 1;
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            return 0;
                        }
                        return 0;
                    }
                })
                .subscribe(new BaseSubscriber<Integer>() {
                    @Override
                    public void onError(ApiException e) {
                        ((ISplashContract.IView)iView).onLoginResult(false);
                    }

                    @Override
                    public void onNext(Integer result) {
                        LogUtils.d("result = " + result);
                        if (result == 1) {
//                            AccountData accountData = AccountConfig.getLoginAccount();
//                            LogUtils.d("account = " +accountData.toString());
                            ((ISplashContract.IView)iView).onLoginResult(true);
                        }else{
                            ((ISplashContract.IView)iView).onLoginResult(false);
                        }
                    }
                });
    }
    @SuppressWarnings("unchecked")
    public void request(HttpParams map, BaseSubscriber subscriber) {
        if (iView == null) {
            LogUtils.e("ivew is null *************");
        }
        Observable observable = iModel.request(Method.POST, false, map);
        observable.compose(iView.bindLifecycle())
                .subscribe(subscriber);
    }
}
